package com.example.leero.graduation.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leero.graduation.API;
import com.example.leero.graduation.App;
import com.example.leero.graduation.BaseActivity;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.StringUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class CommentActivity extends BaseActivity {

    private TextView tv_run_setting;
    private ImageView iv_left;
    private EditText et_content;

    private int act_id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comment;
    }

    @Override
    protected void initVariable() {
        iv_left = (ImageView) findViewById(R.id.iv_left);
        tv_run_setting = (TextView) findViewById(R.id.tv_run_setting);
        et_content = (EditText) findViewById(R.id.et_content);
    }

    @Override
    protected void initView(Bundle state) {
        Intent intent = getIntent();
        act_id = intent.getIntExtra("act_id", 1);

        tv_run_setting.setVisibility(View.VISIBLE);
        tv_run_setting.setText("提交");

        iv_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        tv_run_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtil.isEmpty(et_content.getText().toString())) {
                    postComment();
                } else {
                    Toast.makeText(CommentActivity.this, "评论不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void postComment() {
        OkHttpUtils.post().url(API.COMMENT_ACTIVITY).addParams("act_id", act_id+"")
                .addParams("user_id", App.getLoginUser().id+"")
                .addParams("content", et_content.getText().toString())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
            }
            @Override
            public void onResponse(String response, int id) {
                CommentActivity.this.setResult(1);
                CommentActivity.this.finish();
            }
        });
    }
}
